package shooting.ui
{
	import flash.geom.Point;
	import flash.geom.Rectangle;
	
	import shooting.ui.missile.IMissile;
	
	public class MissileContainer implements IObjectContainer
	{
		private var missileList:Array;
		private var myArea:Rectangle;
		
		public function MissileContainer()
		{
			missileList = new Array;
			myArea = new Rectangle();
		}
		public function setArea(x:Number, y:Number, width:Number, height:Number):void
		{
			myArea.x = x;
			myArea.y = y;
			myArea.width = width;
			myArea.height = height;
		}
		public function get length():Number
		{
			return missileList.length;
		}
		/*public function render():void
		{
			var ms:IMissile;
			for each(ms in missileList)
			{
				ms.render();
				if(!checkInArea(ms.getPosition()) )
				{
					removeList(ms);
				}
			}
		}*/
		public function render():void
		{
			var i:uint;
			var leng:uint = missileList.length;
			for( i = leng; i > 0;)
			{
				i--;
				var ms:IMissile = missileList[i];
				ms.render();
				if(!checkInArea(ms.getPosition()) )
				{
					removeListIndex(i);
				}
			}
		} 
		public function addMissile(ms:IMissile):void
		{
			missileList.push(ms);
		}
		
		public function getList():Array
		{
			return missileList;
		}
		
		private function checkInArea(pt:Point):Boolean
		{
			return myArea.containsPoint(pt);
		}
		 private function removeList(ms:IMissile):void
		{
			var i:uint;
			for(i = missileList.length - 1; i >= 0; i--)
			{
				if(ms == missileList[i])
				{
					trace("before",missileList.length);
					missileList.splice(i,1);
					trace("after",missileList.length);
					break;
				}
			}
		} 
		private function removeListIndex(idx:uint):void
		{
			missileList.splice(idx,1);
		} 
	}
}